#!/bin/bash

# See  ../s/selenium-start  for how to debug.


# Git repo & docs: https://github.com/SeleniumHQ/docker-selenium
#
dash_debug="-debug"
browser_name="chrome"
container_name="tye2ebrowser"

# !! Update Chromedriver and FF driver in wdio.conf.ts too. [upd_chromedriver_ffdriver]
# Not using — see  image_name  below instead.
image_version="4.0.0-alpha-7-prerelease-20201009" # 3.141.59-20201010"
#image_version="selenium/standalone-chrome:4.0.0-beta-4-prerelease-20210527"

# Is 3.5.x and 4.0.0 Selenium Grid? Selenium Server? versions?
#  https://www.selenium.dev/downloads/
# It's "Selenium" version:
# https://selenium-release.storage.googleapis.com/index.html

# not the same as:  selenium/standalone-chrome:86.0-20201009 — then would need
# Chromedriver too in another Docker container?
# This one?:  selenium/standalone-chrome:86.0-chromedriver-86.0-20201009


# A common 14'' laptop resolution is 1920x1200, so this should work
# both on wide screens and not-too-small-screen laptops:
WIDTH=1850
HEIGHT=1100

for arg in "$@"; do
    case $arg in
        k|kill)
        echo
        echo "Killing any $container_name container..."
        echo
        docker kill $container_name
        docker rm -f $container_name
        echo
        echo "Is it gone? These Selenium containers are now running:"
        echo
        docker ps  | grep selenium
        echo
        exit 0
        shift
        ;;
        c|chrome)
        echo "I will use Chrome."
        browser_name="chrome"
        shift
        ;;
        f|ff|firefox)
        echo "I will use Firefox."
        echo "NO bye!"  # fix, to do later
        exit 1
        browser_name="firefox"
        shift
        ;;
        i|invisible)
        echo "The browser will be invisible (headless)."
        echo "NO bye!"  # fix, to do later
        exit 1
        dash_debug=""
        shift
        ;;
        *)
        echo
        echo "Error: Unknown command line argument:  $arg"
        echo
        exit 1
        ;;
    esac
done


# E.g.: selenium/standalone-chrome-debug
#image_name="selenium/standalone-${browser_name}${dash_debug}:${image_version}"
image_name="selenium/standalone-chrome:4.0.0-alpha-7-prerelease-20201009"
#image_name="selenium/standalone-chrome :4.0.0-beta-4-prerelease-20210527"
#image_name="selenium/standalone-$browser_name:4.0.0-beta-4-prerelease-20210604"
#docker pull selenium/standalone-firefox:4.0.0-beta-4-prerelease-20210604
#docker pull selenium/standalone-chrome:4.0.0-beta-4-prerelease-20210604

# docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:4.0.0-alpha-7-prerelease-20201009

# The names are like:
#   selenium/standalone-firefox-debug:3.141.59
#   selenium/standalone-firefox:3.141.59
#   selenium/standalone-chrome:3.141.59
#   selenium/standalone-chrome-debug:3.141.59


echo
echo "Starting image '$image_name' as container '$container_name':"
echo

docker kill $container_name
docker rm -f $container_name

# See: https://github.com/SeleniumHQ/docker-selenium
# Width, height: https://github.com/SeleniumHQ/docker-selenium#setting-screen-resolution
#
docker run -d \
    --name $container_name  \
    --network=host \
    -p 4444:4444 \
    -p 5900:5900 \
    --env SCREEN_WIDTH=$WIDTH \
    --env SCREEN_HEIGHT=$HEIGHT \
    --env VNC_NO_PASSWORD=yes_skip \
    -v /dev/shm:/dev/shm  \
    $image_name


echo
echo "You now have these Selenium containers running:"
echo

docker ps  | grep selenium

echo
echo "You can ssh into the container:"
echo
echo "    docker exec -it $container_name sh"
echo

install_vinagre="sudo apt install vinagre"
start_vinagre="vinagre --geometry=${WIDTH}x${HEIGHT} 127.0.0.1:5900"

if [ -z "$dash_debug" ]; then
    echo "The browser is invisible; you cannot connect with VNC."
else
    echo "You can look at the browser via VNC, using e.g. Vinagre, install and start like so:"
    echo
    echo "    $install_vinagre"
    echo "    $start_vinagre"

    if [ -z "$(which vinagre)" ]; then
        echo
        read -r -p "Shall I install Vinagre for you? [y/N] " response
        response=${response,,}    # tolower
        if [[ ! $response =~ ^(yes|y)$ ]] ; then
            echo "No? Bye."
            exit 0
        fi
        echo "Yes, here we go:"
        echo
        set -x
        $install_vinagre
        set +x
        echo
    fi

    echo
    read -r -p "Shall I start Vinagre for you? [Y/n] " response
    response=${response,,}    # tolower
    if [[ $response =~ ^(no|n)$ ]] ; then
        echo "Bye."
        exit 0
    fi
    echo "Ok. Waiting for the VNC server on port 5900 ..."
    echo

    # Apparently Netcat (nc) exits with status ok, if the VNC server closes
    # the connection when one talks about teapots?  But nc exits with error,
    # if cannot connect at all?
    while ! echo "I am a teapot" | nc 127.0.0.1 5900; do
      sleep 1
    done

    echo
    echo "... Now ready. Starting Vinagre:"
    echo
    echo "$start_vinagre"
    echo

    sleep 1
    $start_vinagre
fi
